/**
 * Created by DevilsEyes on 2016/2/13.
 */
'use strict';

var express = require('express');
var router = express.Router();

var $user = require('../midware/user');
var $session = require('../midware/session');

//判断登录状态
router.get('/login', function (req, res, next) {

    let cookies = req.cookies;
    let ssid = cookies.ssid;

    let NotLogged = ()=> {
        res.json({
            code: 0,
            login: false
        });
    };

    if (ssid === undefined) {
        return NotLogged();
    } else {
        var Se = $session.getBySid(ssid);
        if (Se) {
            $user.searchByMd5Id(Se.md5_userId)
                .then((user)=> {
                    if (user) {
                        res.json({
                            code: 0,
                            user: $user.sync_filter(user)
                        });
                    } else {
                        NotLogged();
                    }
                })
                .catch((e)=> {
                    NotLogged();
                });
        } else {
            NotLogged();
        }

    }

});

//登出
router.delete('/login', function (req, res, next) {

    res.clearCookie("ssid", {path: '/'});
    res.json({
        code: 0,
        msg: '登出成功!'
    })

});

//登录
router.post('/login', function (req, res, next) {

    let data = req.body;
    //sid
    //password

    console.log(data);

    new Promise((O, X)=> {
        O();
    })
        .then(()=> {

            if (!data.sid) {
                throw {
                    code: 9999,
                    msg: '没有加密密码'
                };
            }

            if (!data.password) {
                throw {
                    code: 9999,
                    msg: '没有密码'
                };
            }

            return $user.searchByMd5Id(data.sid)
        })
        .then((user)=> {

            if (user == null) {
                throw{
                    code: 9999,
                    msg: '用户不存在'
                };
            }

            if (data.password != user.secPassword) {
                throw{
                    code: 9999,
                    msg: '密码错误'
                };
            }

            var ssid = $session.setSession(user);
            res.cookie("ssid", ssid);
            res.json({
                code: 0,
                user: $user.sync_filter(user)
            });
        })
        .catch((e)=> {
            res.json({
                code: e.code,
                msg: e.msg
            });
        });

});

//获取验证状态
router.get('/verify', function (req, res, next) {

    let _id = req.query._id;

    $user.getUserByMongoId(_id)
        .then((user)=> {
            if (user) {
                if (user.role != -1) {
                    res.json({
                        code: 9999,
                        msg: '无效的验证凭据'
                    });
                }
                else {
                    res.json({
                        code: 0,
                        user: $user.sync_filter(user)
                    });
                }
            } else {
                res.json({
                    code: 9999,
                    msg: '没有这名同学'
                });
            }
        })
        .catch((e)=> {
            res.json({
                code: e.code,
                msg: e.msg
            });
        });
});

//补全用户信息(完成注册)
router.post('/verify', function (req, res, next) {
    let md5_id = req.body.secId;
    let nickname = req.body.nickname;
    let password = req.body.password;
    let User = null;
    new Promise((O, X)=> {
        if (!md5_id)X({code: 9999, msg: '没有学号'});
        if (!nickname)X({code: 9999, msg: '没有昵称'});
        if (!password)X({code: 9999, msg: '没有密码'});

        O($user.searchByMd5Id(md5_id));
    })
        .then((user)=> {
            if (!user || user.role != -1)X({code: 9999, msg: '没有该学生'});
            User = user;
            User.nickname = nickname;
            User.password = password;
            return $user.complete(User);
        })
        .then(()=> {
            var ssid = $session.setSession(User);
            res.cookie("sid", ssid);
            res.json({
                code: 0,
                user: $user.sync_filter(User)
            });
        })
        .catch((e)=> {
            res.json({
                code: e.code,
                msg: e.msg
            });
        });
});

//注册
router.post('/regist', function (req, res, next) {

    let data = req.body;

    new Promise((O, X)=> {
        O();
    })
        .then(()=> {

            if (!data.id) throw {code: 9999, msg: '没有学号'};
            if (!data.name) throw {code: 9999, msg: '没有真实姓名'};
            if (!data.email) throw {code: 9999, msg: '没有邮箱'};
            if (!data.class) throw {code: 9999, msg: '没有班级'};

            return $user.regist(data)
        })
        .then(()=> {
            res.json({
                code: 0
            });
        })
        .catch((e)=> {
            res.json({
                code: e.code,
                msg: e.msg
            });
        });
});

module.exports = router;